﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Databases
{
    public partial class Form1 : Form
    {
        private DAL data;
        private DataAccessQueriesReader qr;

        private bool firstTransferTabLoad;

        public Form1()
        {
            InitializeComponent();
            data = new DAL();
            qr = new DataAccessQueriesReader();
            //Load the locations
            LoadLocations();
            getWeaponInventory();
            firstTransferTabLoad = true;
        }

        public void LoadLocations()
        {
            DataSet ds = data.getDataSet(qr.getSelect("getLocations"));
            cmbLocations.DataSource = ds.Tables[0].DefaultView;
            cmbLocations.DisplayMember = "LocationName";
            cmbLocations.ValueMember = "LocationID";
            cmbLocations.SelectedValue = 1;
        }

        private void cmbLocation_OnChange(object sender, EventArgs e)
        {
            getWeaponInventory();
        }

        private void getWeaponInventory()
        {
            List<OleDbParameter> parameters = new List<OleDbParameter>();
            OleDbParameter param = new OleDbParameter("@LocationID", cmbLocations.SelectedValue);
            parameters.Add(param);

            DataSet ds = data.getDataSetWithParameters(qr.getSelect("getInvByLocID"), parameters);
            dgvInventoryWeapons.DataSource = ds.Tables[0];
        }

        private void pageTab_Selected(object sender, TabControlEventArgs e)
        {
            TabControl t = sender as TabControl;
            switch (t.SelectedIndex)
            {
                case 2:
                    if (firstTransferTabLoad)
                    {
                        LoadTransfersTab();
                        firstTransferTabLoad = false;
                    }
                    break;
                default:
                    break;
            }
        }

        private void LoadTransfersTab()
        {
            LoadTransferFrom();
            LoadAvailableVehicles();
            LoadTransferTo();
        }

        private void LoadTransferFrom()
        {
            DataSet ds = data.getDataSet(qr.getSelect("getLocations"));
            cmbFrom.DataSource = ds.Tables[0].DefaultView;
            cmbFrom.DisplayMember = "LocationName";
            cmbFrom.ValueMember = "LocationID";
            cmbFrom.SelectedValue = 1;
        }

        private void LoadTransferTo()
        {
            List<OleDbParameter> parameters = new List<OleDbParameter>();
            OleDbParameter param = new OleDbParameter("@LocationID", cmbFrom.SelectedValue);
            parameters.Add(param);
            DataSet ds = data.getDataSetWithParameters(qr.getSelect("getLocationsWOPassedID"), parameters);
            cmbTo.DataSource = ds.Tables[0].DefaultView;
            cmbTo.DisplayMember = "LocationName";
            cmbTo.ValueMember = "LocationID";
            cmbTo.SelectedIndex = 0;
        }

        private void LoadAvailableVehicles()
        {
            List<OleDbParameter> parameters = new List<OleDbParameter>();
            OleDbParameter param = new OleDbParameter("@LocationID", cmbFrom.SelectedValue);
            parameters.Add(param);
            DataSet ds = data.getDataSetWithParameters(qr.getSelect("getVehiclesByLocationID"), parameters);
            lstVehiclesAvailable.DataSource = ds.Tables[0].DefaultView;
            lstVehiclesAvailable.DisplayMember = "VehicleString";
            lstVehiclesAvailable.ValueMember = "VehicleID";
        }

        private void cmbFrom_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!firstTransferTabLoad)
            {
                LoadAvailableVehicles();
                LoadTransferTo();
            }
        }
    }
}
